package com.djt.book_system.controller;

import com.djt.book_system.entity.Books;
import com.djt.book_system.entity.Borrows;
import com.djt.book_system.entity.BorrowsInfo;
import com.djt.book_system.service.BooksService;
import com.djt.book_system.service.BorrowsService;
import com.djt.book_system.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * (Borrows)表控制层
 *
 * @author makejava
 * @since 2021-05-19 10:48:10
 */
@RestController
@RequestMapping("borrows")
public class BorrowsController {
    /**
     * 服务对象
     */
    @Resource
    private BorrowsService borrowsService;
    @Resource
    private BooksService booksService;
    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Borrows selectOne(Integer id) {
        return this.borrowsService.queryById(id);
    }

    @PostMapping("addOne")
    public void addOne(int[] bid , int uid){

        Borrows borrows = new Borrows();
        //被借阅书id
        borrows.setBid(bid[0]);
        //借阅人
        borrows.setUid(uid);
        //添加借阅记录
        borrowsService.insert(borrows);
        //借阅（修改书籍库存）
        borrowsService.borrows(bid);
    }
    @PostMapping("batchAdd")
    public void batchAdd(int[] bids,int uid){
        Borrows borrows = new Borrows();
        for (int bid:bids
             ) {
            borrows.setBid(bid);
            borrows.setUid(uid);
            borrowsService.insert(borrows);
        }
        borrowsService.borrows(bids);
    }
    @GetMapping("select")
    public Result select(int page,int limit,BorrowsInfo conditional){
        int offset = (page-1)*limit;
        List<BorrowsInfo> borrowsInfoList = borrowsService.queryBorrowsInfo(offset,limit,conditional);
        int count = borrowsService.borrowsCount(conditional);
        if (borrowsInfoList.size()==0){
            try {
                offset=(page-1-1)*limit;
                borrowsInfoList = borrowsService.queryBorrowsInfo(offset,limit,conditional);
                return Result.success(count,borrowsInfoList);
            }catch (Exception e){
                return Result.success(0,null);
            }
        }
        return Result.success(count,borrowsInfoList);
    }
    @GetMapping("selectByUser")
    public Result selectByUser(int page,int limit,BorrowsInfo conditional){
        if (conditional.getBotime() != null && conditional.getBotime() != ""){
            String botime_start = conditional.getBotime().substring(0,19);
            String botime_end = conditional.getBotime().substring(22,40);
            conditional.setBotime_start(botime_start);
            conditional.setBotime_end(botime_end);
        }
        return this.select(page,limit,conditional);
    }
    @GetMapping("back")
    public void back(Integer boid , Integer bid){
        borrowsService.back(boid);
        borrowsService.backbook(bid);
    }
}
